package com.csd.serviceImpl;

import java.util.List;

import com.csd.dao.ProjectDAO;
import com.csd.dao.StudentDAO;
import com.csd.dao.TeamDAO;
import com.csd.exception.*;
import com.csd.helper.SessionManager;
import com.csd.pojo.Student;
import com.csd.pojo.Team;
import com.csd.service.IStudentService;

/**
 * 
 * Service：学生
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：刘世彬
 * 
 * <br> 
 * 
 */
public class StudentService implements IStudentService {

	private StudentDAO studentDAO;
	private TeamDAO teamDAO;

	private ProjectDAO projectDAO;

	public TeamDAO getTeamDAO() {
		return teamDAO;
	}

	public void setTeamDAO(TeamDAO teamDAO) {
		this.teamDAO = teamDAO;
	}

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public StudentDAO getStudentDAO() {
		return studentDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	/**
	 * 根据团队查询成员
	 * @param team
	 * @return List<Student>
	 */
	public List<Student> listStudentByTeam(Team team) {
		List<Student> students = null;
		try {
			students = studentDAO.findByProperty("team", team);
		} catch (RuntimeException re) {
			throw new GetException(re, "查询团队的成员异常");
		}
		return students;
	}
	
	/**
	 * 向团队添加一个成员
	 * @param member
	 * @return boolean
	 */
	public boolean addMember(Student member) {
		boolean tag = false;

		try {

			int id = SessionManager.getUserID();
			List list = projectDAO.findByTeam(id);
			if (list == null || (list != null && list.size() == 0)) {
				Team team = new Team();
				team.setId(id);
				member.setTeam(team);
				studentDAO.save(member);

				// 如果团队只有一个成员,设置它为队长
				team = teamDAO.findById(id);
				List studentList = studentDAO.findByProperty("team", team);
				if (studentList != null && studentList.size() == 1) {
					team = teamDAO.findById(id);
					team.setLeader((Student) studentList.get(0));
					teamDAO.update(team);
				}
			}
			tag = true;
		} catch (RuntimeException re) {
			throw new SaveException(re, "向团队添加成员异常");
		}

		return tag;
	}

	/**
	 * 删除一个成员
	 * @param ID
	 * @return boolean
	 */
	public boolean deleteMember(Integer ID) {
		boolean tag = false;

		try {
			int id = SessionManager.getUserID();
			List list = projectDAO.findByTeam(id);
			if (list == null || (list != null && list.size() == 0)) {
				Student member = studentDAO.findById(ID);
				studentDAO.delete(member);
			}
			tag = true;

		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除团队成员异常");
		}

		return tag;
	}


	/**
	 * 根据ID查询成员信息
	 * @param ID
	 * @return Student
	 */
	public Student getMemberByID(Integer ID) {
		Student member = null;

		try {
			member = studentDAO.findById(ID);

		} catch (RuntimeException re) {
			throw new GetException(re, "查询成员信息异常");
		}

		return member;
	}
	
	/**
	 * 查询一个团队的成员
	 * @param teamID
	 * @return List<Student>
	 */
	public List<Student> listMemberByTeam(Integer teamID) {
		List<Student> list = null;

		try {
			list = studentDAO.findByProperty("team.id", teamID);

		} catch (RuntimeException re) {
			throw new GetException(re, "列出团队成员异常");
		}
		return list;
	}
	
	/**
	 * 团队登录之后，查询团队成员
	 * @param 
	 * @return List<Student>
	 */
	public List<Student> listTeamMember() {
		List<Student> list = null;

		try {
			int teamID = SessionManager.getUserID();
			list = studentDAO.findByProperty("team.id", teamID);

		} catch (RuntimeException re) {
			throw new GetException(re, "列出自己团队成员异常");
		}
		return list;
	}

	/**
	 * 更新团队成员
	 * @param member
	 * @return boolean
	 */
	public boolean updateMember(Student member) {
		boolean tag = false;

		try {
			studentDAO.update(member);
			tag = true;
		} catch (RuntimeException re) {
			throw new UpdateException(re, "更新团队成员信息异常");
		}

		return tag;
	}

}
